import Vue from 'vue'
import Vuex from 'vuex'
// import axios from 'axios'
import http from '../ajax/http'
import * as url from '../api'
Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    user:{
      userName:localStorage.getItem('userName')||undefined,
      token:localStorage.getItem('token')||undefined,
      type:localStorage.getItem('type')||undefined
    },

  },
  mutations: {
    addUser(state,obj){
      console.log(obj.token)
      state.user.userName=obj.data.user
      state.user.token=obj.token
      state.user.type=obj.data.type
    }
  },
  actions: {
    loginUser({commit},obj){
      console.log(obj)
      // console.log(commit)
      let {user,pwd}=obj
      return http({
        url:url.LOGIN,
        data:{user,pwd},
        method:'post',
      }).then(res=>{
        console.log(res)//返回的数据
        if(res.data.code===200){
          console.log(res.data)
          commit('addUser',res.data)
          localStorage.setItem('userName',res.data.data.user)
          localStorage.setItem('type',res.data.data.type)

        }
        return res.data
      })
    }
  },
  modules: {
  }
})
